package it.xiuxian.lib.utils;


import android.os.Build;

import androidx.annotation.RequiresApi;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Random;
import android.util.Base64;

public class AESUtils{

    /**
     * 加密Key 需要16位 可用数字与字母组成
     */
    private static String key = "Ijasw2o93jd81kdD";
    /**
     * 偏移量 需要16位
     */
    private static String iv = "4w2Df1xSj5ff662d";

    private static final String ecbkey = "leidun_ecology_a";

   /* private static Base64 decoder;

    private static Base64.Encoder encoder;*/



   /* static {
        decoder = Base64.getDecoder();
        encoder = Base64.getEncoder();
    }*/



    public static String getSixteenBitString(){
        StringBuffer sb = new StringBuffer();
        String[] chars = new String[]{
                "1","2","3","4","5","6","7","8","9","a","b",
                "c","d","e","f","g","h","i","j","k","l","m",
                "n","o","p","q","r","s","t","u","v","w","x",
                "y","z","A","B","C","D","E","F","G","H","I",
                "J","K","L","M","N","O","P","Q","R","S","T",
                "U","V","W","X","Y","Z",
        };
        int len = chars.length;
        Random random = new Random();
        for (int i = 0; i < 16; i++) {
            sb.append(chars[random.nextInt(len-1)]);
        }
        return sb.toString();

    }

    /**
     * AES加密
     * @param data
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */
    public static String encryptAES_CBC(String data,String key,String iv){
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/NoPadding");

            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = data.getBytes();
            int dataLength = dataBytes.length;
            if (dataLength % blockSize != 0) {
                dataLength = dataLength + (blockSize - (dataLength % blockSize));
            }
            byte[] plaintext = new byte[dataLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
            byte[] bytes = cipher.doFinal(plaintext);
            return Base64.encodeToString(bytes,2);
            //return encoder.encodeToString(bytes);

        }catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }
    /**
     * AES解密
     * @param data
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */
    public static String decrptyAES_CBC(String data,String key,String iv){
        try {
            byte[] bytes = Base64.decode(data,2);
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] plainByte = cipher.doFinal(bytes);
            return new String(plainByte).trim();
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }


    /**
     * AES加密
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptAES_ECB(String data) throws Exception{
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        int blockSize = cipher.getBlockSize();
        byte[] dataBytes = data.getBytes();
        int dataLength = dataBytes.length;
        if(dataLength % blockSize != 0){
            dataLength = dataLength + (blockSize - (dataLength % blockSize));
        }
        byte [] plaintext = new byte[dataLength];
        System.arraycopy(dataBytes,0,plaintext,0,dataBytes.length);
        SecretKeySpec secretKey = new SecretKeySpec(ecbkey.getBytes(),"AES");
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
        byte[] bytes = cipher.doFinal(plaintext);
        return Base64.encodeToString(bytes,2);
    }

    /**
     * AES解密
     * @param data
     * @return
     * @throws Exception
     */
    public static String decrptyAES_ECB(String data) throws Exception{
        byte[] bytes = Base64.decode(data,2);
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        SecretKeySpec secretKey = new SecretKeySpec(ecbkey.getBytes(),"AES");
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        byte[] plainByte = cipher.doFinal(bytes);
        return new String(plainByte).trim();
    }

   /* public static void main(String[] args) {
        try {
            String jiami = "{\"loginname\":\"安达市大所安达市大所安达市大所安达市大所安达市大所\"}";

            //if ("nMZYBp0+m4KEzUF2WizPIsXRJxSAWZNAQOztmWwcZKI=".equals(""))

            //String map = decrptyAES_ECB("nMZYBp0%2Bm4KEzUF2WizPIsXRJxSAWZNAQOztmWwcZKI=");
            System.out.println(encryptAES_ECB(jiami));

            System.out.println(decrptyAES_ECB(encryptAES_ECB(jiami)));

        }catch (Exception e){
            e.printStackTrace();
        }
    }*/

}
